package com.systemsjr.mpf;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.systemsjr.mpf.account.Account;
import com.systemsjr.mpf.account.ManageAccountActivity;
import com.systemsjr.mpf.expense.ManageExpenseActivity;
import com.systemsjr.mpf.expense.ManageExpenseTypeActivity;

public class MpfActivity extends ListActivity{

	private SimpleCursorAdapter adapter;
	private ListView listView;
	private Cursor c;
	

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mpf);
				
		String[] from = { Account.CODE, Account.ACCOUNT };
		Account acc = new Account();
		c = getContentResolver().query(Account.CONTENT_URI, Account.PROJECTIONS, null, null, null);
		String[] selectionArgs = {"%", "%"};
		
		Bundle bundle = getIntent().getExtras();
		if(bundle != null && bundle.size() >0){
			if(bundle.getString(Account.TABLE_NAME) != null){
				acc = (Account) bundle.get(Account.TABLE_NAME);
			}
			
			selectionArgs = new String[]{acc.getCode() == null || acc.getCode().length() == 0 ? "%" : "%" + acc.getCode() + "%", 
					acc.getAccount() == null || acc.getAccount().length() == 0 ? "%" : "%" + acc.getAccount() + "%"};
			
		}
	
		String selection = "CODE LIKE ? AND ACCOUNT LIKE ?";
				
		c = getContentResolver().query(Account.CONTENT_URI, Account.PROJECTIONS, selection, selectionArgs, null);
		
		startManagingCursor(c);

		int[] to = { R.id.code_entry, R.id.account_entry};

		adapter = new SimpleCursorAdapter(this, R.layout.account_list_entry, c, from, to, 0);
		listView = getListView();

		listView.setAdapter(adapter);

		Button addAccountButton = (Button) findViewById(R.id.addAccountButton);
		addAccountButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(MpfActivity.this,
						ManageAccountActivity.class);
				startActivity(intent);
			}
		});
		
		Button expenseButton = (Button) findViewById(R.id.addExpenseButton);
		expenseButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(MpfActivity.this, ManageExpenseActivity.class);
				startActivity(intent);
				
			}
		});

	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent intent = new Intent(MpfActivity.this, ManageAccountActivity.class);

		if (adapter != null) {
			Cursor c = (Cursor) adapter.getItem(position);
			intent.putExtra(Account.CODE, c.getString(c.getColumnIndex(Account.CODE)));
			intent.putExtra(Account.ID, c.getLong(c.getColumnIndex(Account.ID)));
			intent.putExtra(Account.ACCOUNT, c.getString(c.getColumnIndex(Account.ACCOUNT)));
		}
		startActivityForResult(intent, 1);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.mpf, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		Intent intent = IntentHandler.handleOnOptionsItemSelected(this, item.getItemId());
		startActivityForResult(intent, 1);
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		//c.requery();
		adapter.notifyDataSetChanged();
		listView.invalidateViews();
	}
}
